#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: type_mapping_use_excel.py 
@version:
@time: 2021/09/14 
"""
import json

from TapClientCore.T01_init_conf.path_conf import PathConf
from TapUtil.operatefile.use_excel import UtilExcel
from TapUtil.operatefile.use_json import jsonfile2python


class TypeMappingUseExcel(UtilExcel):
    raw_data = jsonfile2python(PathConf.TYPE_MAPPING_JSON_RESULT)

    def del_sheet(self, sheet='Sheet'):
        del self.wb[sheet]

    async def create_type_mapping_sheet(self):
        for i in self.raw_data:
            self.create_sheet(i)

    async def type_mapping_write_data(self):
        for dbtype in self.raw_data:
            # 每种源数据重置为1
            column = 1
            for one_mission in self.raw_data[dbtype]:
                print(one_mission)
                #
                row = 1
                for k, v in one_mission.items():
                    if column == 1:
                        self.write_data(sheet=dbtype, row=row, column=column, value=f"{dbtype}_source")
                        for info in v[:-1]:
                            row += 1
                            self.write_data(sheet=dbtype, row=row, column=column, value=json.dumps(info[0]))
                            print(info[0])
                        row = 1
                    column += 1
                    self.write_data(sheet=dbtype, row=row, column=column, value=k)
                    for info in v[:-1]:
                        row += 1
                        self.write_data(sheet=dbtype, row=row, column=column, value=json.dumps(info[1]))
                        print(info[1])
            # self.write_data(sheet=dbtype, row=row, column=column, value=one_mission)
            # column += 1

    async def type_mapping_write_data_only_type(self):
        for dbtype in self.raw_data:
            # 每种源数据重置为1
            column = 1
            for one_mission in self.raw_data[dbtype]:
                print(one_mission)
                #
                row = 1
                for k, v in one_mission.items():
                    if column == 1:
                        self.write_data(sheet=dbtype, row=row, column=column, value=f"{dbtype}_source")
                        for info in v[:-1]:
                            row += 1
                            self.write_data(sheet=dbtype, row=row, column=column, value=json.dumps(info[0][1]))
                            print(info[0])
                        row = 1
                    column += 1
                    self.write_data(sheet=dbtype, row=row, column=column, value=k)
                    for info in v[:-1]:
                        row += 1
                        self.write_data(sheet=dbtype, row=row, column=column, value=json.dumps(info[1][1]))
                        print(info[1])
            # self.write_data(sheet=dbtype, row=row, column=column, value=one_mission)
            # column += 1

    async def type_mapping_write_data_only_scope(self):
        for dbtype in self.raw_data:
            # 每种源数据重置为1
            column = 1
            for one_mission in self.raw_data[dbtype]:
                print(one_mission)
                #
                row = 1
                for k, v in one_mission.items():
                    if column == 1:
                        self.write_data(sheet=dbtype, row=row, column=column, value=f"{dbtype}_source")
                        for info in v[:-1]:
                            row += 1
                            self.write_data(sheet=dbtype, row=row, column=column,
                                            value=json.dumps((info[0][1], info[0][2])))
                            print(info[0])
                        row = 1
                    column += 1
                    self.write_data(sheet=dbtype, row=row, column=column, value=k)
                    for info in v[:-1]:
                        row += 1
                        self.write_data(sheet=dbtype, row=row, column=column, value=json.dumps(info[1][2]))
                        print(info[1])
            # self.write_data(sheet=dbtype, row=row, column=column, value=one_mission)
            # column += 1
